home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / misc / sci / RARS_Amiga_2.lha / RARS / cntrl2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-31  |  3.1 KB  |  89 lines

  1. // CNTRL2.CPP - "driver" function for RARS - M. Timin, Dec. 1994
  2. // (see CNTRL0.CPP for better comments)
  3. // adapted to ver. 0.39 3/6/95 by M. Timin
  4.  
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #include "car.h"
  9.  
  10. extern const double CARWID;
  11. extern char* glob_name;
  12.  
  13. con_vec cntrl2(situation s)
  14. {
  15.    const char name[] = "Ritchie";
  16.    static int init_flag = 1;
  17.    con_vec result;
  18.    double alpha, vc;
  19.    double width;                      // track width, feet
  20.    double steer_gain = .18;
  21.    const double steer_damp = .48;
  22.    static int lane_time = 0;           // counts time when not in normal lane
  23.    const double normalane = 60.0;
  24.    static double lane = 60;    // determines right-left position on straigtaway
  25.    const double corn_con = 6.4;
  26.    static double corn_spd = 55.0;
  27.  
  28.    if(init_flag)  {
  29.       strcpy(glob_name, name);
  30.       init_flag = 0;
  31.       result.alpha = result.vc = 0;
  32.       return result;
  33.    }
  34.  
  35.   if(stuck(s.backward, s.v,s.vn, s.to_lft,s.to_rgt, &result.alpha,&result.vc))
  36.       return result;
  37.  
  38.    if(s.cur_rad == 0.0 && s.nex_rad != 0.0)
  39.       corn_spd = corn_con * sqrt(s.nex_rad > 0.0 ? s.nex_rad : -s.nex_rad);
  40.    else
  41.       corn_spd = corn_con * sqrt(s.cur_rad > 0.0 ? s.cur_rad : -s.cur_rad);
  42.  
  43.    // maybe choose a different lane: (to help in passing)
  44.    if(!s.dead_ahead) {
  45.       if(lane_time <= 0) {
  46.          lane = normalane;
  47.          lane_time = 0;
  48.       }
  49.    }
  50.    else if(!lane_time)   {
  51.       // pick a different lane:
  52.       if(lane >= 80)               // pick a new lane somehow:
  53.          lane = random(60) - 20;
  54.       else if(lane <= -80)
  55.          lane = 20 - random(60);
  56.       else
  57.          lane = random(180) - 90;
  58.       lane_time = 175;              // we will stay in the new lane this long
  59.    }
  60.    if(lane_time > 0)
  61.       --lane_time;
  62.  
  63.    width = s.to_lft + s.to_rgt;                        // find width of track
  64.    if(s.cur_rad == 0)                                                  
  65.       alpha = .25 * steer_gain * (s.to_lft - s.to_rgt - lane) / width;   
  66.    else if(s.cur_rad > 0)
  67.       alpha = steer_gain * (4 * (s.to_lft-1.6*CARWID) / width
  68.                                                        + .2 * width/s.to_rgt);
  69.    else
  70.       alpha = -steer_gain * (4 * (s.to_rgt-1.6*CARWID) / width
  71.                                                        + .2 * width/s.to_lft);
  72.    alpha -= steer_damp * s.vn / s.v;  // This is damping, to prevent oscillation
  73.  
  74.    if(s.cur_rad == 0)         // If we are on a straightaway,
  75.       if(s.to_end/s.cur_len > .38)      // if we are far from the end:
  76.          vc = s.v + 100/s.v;        // keep accellerating near full power
  77.       else  {                 // otherwise,
  78.          // maintain cornering speed, braking at first
  79.          vc = corn_spd + 9.0 * s.to_end/s.cur_len;
  80.          // this next formala will not work if a rt. turn follows the straight:
  81.          alpha += .4 * (s.cur_len/(s.to_end + s.cur_len) - (1/1.38));
  82.       }
  83.    else            // if we're in the curve, maintain speed, +
  84.       vc = corn_spd + .9 / (s.to_end + .4);
  85.  
  86.    result.vc = vc;   result.alpha = alpha;
  87.    return result;
  88. }                                                              // v;
  89.